package com.sicilin.system.auth.action;

import com.sicilin.common.ajax.vo.AjaxResponse;
import com.sicilin.common.enums.ResultStateEnum;
import com.sicilin.common.exception.BusinessException;
import com.sicilin.common.json.easyui.vo.TreegridResponse;
import com.sicilin.po.TSysAuth;
import com.sicilin.system.auth.dto.TreeAuth;
import com.sicilin.system.auth.service.AuthService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/authAction")
public class AuthAction {

    @Autowired
    private AuthService authService;

    @RequestMapping(value = "/getTreeAuth.json",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("system:auth:search")
    public TreegridResponse<TreeAuth> getTreeAuth()throws Exception{
        List<TreeAuth> auths = authService.getTreeAuth();
        if( auths == null || (auths != null && auths.size() == 1 && auths.contains(null))){
            auths = new ArrayList<TreeAuth>();
        }
        TreeAuth rootAuth = new TreeAuth();
        rootAuth.setAuthId(0);
        rootAuth.setAuthCode("tms");
        rootAuth.setAuthName("tms");
        auths.add(rootAuth);
        return new TreegridResponse<TreeAuth>(auths.size(),auths);
    }

    @RequestMapping(value = "/saveAuth.json",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions({"system:auth:save","admin"})
    public AjaxResponse saveAuth(@RequestBody TSysAuth auth) throws Exception{
        if(StringUtils.isBlank(auth.getAuthCode())){throw new BusinessException("无效的权限编码!");}
        if(StringUtils.isBlank(auth.getAuthName())){throw new BusinessException("无效的权限名称!");}
        if(auth.getParentId() == null){throw new BusinessException("无效的父级！");}
        if(auth.getLevel() == null){throw new BusinessException("无效的权限等级！");}
        if(auth.getAuthType() == null){throw new BusinessException("无效的权限类型！");}
        return new AjaxResponse(ResultStateEnum.instances(authService.saveAuth(auth.getAuthCode(),auth.getAuthName(),auth.getParentId(),auth.getAuthType(),auth.getLevel())));
    }

    @RequestMapping(value = "/deleteAuth.json",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions({"system:auth:delete","admin"})
    public AjaxResponse deleteAuth(@RequestParam("authId") int authId) throws Exception{
        if(authId == 0){throw new BusinessException("权限选择错误！");}
        return new AjaxResponse(authService.deleteAuth(authId));
    }

    @RequestMapping(value = "/updateAuth.json",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions({"system:auth:update","admin"})
    public AjaxResponse updateAuth(@RequestBody TSysAuth auth) throws Exception{
        if(auth.getAuthId() == null){throw new BusinessException("没有选择权限!");}
        if(StringUtils.isBlank(auth.getAuthName())){throw new BusinessException("无效的权限名称!");}
        if(auth.getAuthType() == null){throw new BusinessException("无效的权限类型！");}
        if(auth.getLevel() == null){throw new BusinessException("无效的权限等级！");}
        return new AjaxResponse(ResultStateEnum.instances(authService.updateAuth(auth.getAuthId(),auth.getAuthName(),auth.getAuthType(),auth.getLevel())));
    }


    @RequestMapping(value = "/getTreeAuthWidthCheckedByRoleId.json",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("system:role:search")
    public TreegridResponse<TreeAuth> getTreeAuthWidthCheckedByRoleId(@RequestParam("roleId") int roleId, HttpServletResponse response) throws Exception{
        List<TreeAuth> auths = authService.getTreeAuthWidthCheckedByRoleId(roleId);
        TreeAuth rootAuth = new TreeAuth();
        rootAuth.setAuthId(0);
        rootAuth.setAuthCode("tms");
        rootAuth.setAuthName("tms");
        auths.add(rootAuth);
        TreegridResponse<TreeAuth> treeAuth = new TreegridResponse<TreeAuth>(auths.size(),auths);
        return treeAuth;
    }

}
